package com.yearEndWorkload.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.yearEndWorkload.common.Result;
import com.yearEndWorkload.listen.ExcelResProp;
import com.yearEndWorkload.listen.ServicePerformanceListener;
import com.yearEndWorkload.pojo.entity.ServicePerformance;
import com.yearEndWorkload.pojo.excel.importTemplate.ServicePerformanceExcel;
import com.yearEndWorkload.pojo.form.add.AddServicePerformanceForm;
import com.yearEndWorkload.pojo.form.update.UpdateServicePerformanceForm;
import com.yearEndWorkload.service.ServicePerformanceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/s3/servicePerformance")
@Tag(name = "管理服务绩效管理", description = "管理服务绩效管理")
//TODO 已有生成的serviceImpl
public class ServicePerformanceController {
    @Resource
    private ServicePerformanceService servicePerformanceService;

    @Operation(summary = "添加一个服务绩效")
    @PostMapping("/addServicePerformance")
    public Result<?> addServicePerformance(@RequestBody AddServicePerformanceForm addServicePerformanceForm) {
        return servicePerformanceService.addServicePerformance(addServicePerformanceForm);
    }

    @Operation(summary = "批量删除服务绩效")
    @DeleteMapping("/deleteServicePerformance")
    public Result<?> deleteServicePerformance(@RequestParam("ids") Integer[] ids) {
        return servicePerformanceService.deleteServicePerformance(ids);
    }

    @Operation(summary = "查询所有服务绩效")
    @GetMapping("/getServicePerformanceList")
    public Result<List<ServicePerformance>> getServicePerformanceList(Long pageNumber, Long pageSize) {
        return servicePerformanceService.getServicePerformanceList(pageNumber, pageSize);
    }

    @Operation(summary = "更新一个服务绩效")
    @GetMapping("/updateServicePerformanceList")
    public Result<?> updateServicePerformance(UpdateServicePerformanceForm updateServicePerformanceForm) {
        return servicePerformanceService.updateServicePerformance(updateServicePerformanceForm);
    }

    @Operation(summary = "批量导出服务绩效")
    @PostMapping("/addByBatch")
    public Result<?> addByBatch(@RequestParam("file") MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), ServicePerformanceExcel.class,new ServicePerformanceListener(servicePerformanceService)).sheet().doRead();
        return Result.success(null);
    }

    @Operation(summary = "批量导出服务绩效")
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        ExcelResProp.setExcelRespProp(response, "服务绩效");
        List<ServicePerformance> servicePerformanceList = servicePerformanceService.list();
        EasyExcel.write(response.getOutputStream())
                .head(ServicePerformance.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("服务绩效")
                .doWrite(servicePerformanceList);
    }
}
